package com.zzh.ssi.frame.base;

import com.opensymphony.xwork2.ActionSupport;
import com.zzh.ssi.frame.util.PageInfo;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * Date: 13-1-10
 * Time: 下午4:25
 * To change this template use File | Settings | File Templates.
 */
public class BaseAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {
    protected Logger log = Logger.getLogger(this.getClass());
    protected String key;
    protected String sort;
    private String token;
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected PageInfo pageInfo = new PageInfo();

    /**
     * request
     *
     * @return
     */
    public HttpServletRequest getRequest() {
        return request;
    }

    /**
     * 获取Session对象
     *
     * @return
     */
    public HttpSession getSession() {
        return request.getSession();
    }

    /**
     * 获取Response对象
     *
     * @return
     */
    public HttpServletResponse getResponse() {
        return response;
    }

    public PrintWriter getPrintWriter() throws IOException {
        return response.getWriter();
    }

    public void writeJson(String json) throws IOException {
        response.setContentType("text/html; charset=utf-8");
        PrintWriter pw = response.getWriter();
        pw.print(json);
    }

    /**
     * 检查是否重复提交
     *
     * @return true:重复提交，false：新的提交
     */
    public boolean isAginSubmit() {
        boolean flag = false;
        String sessionToken = (String) request.getSession().getAttribute("sessionToken");
        if (null == sessionToken || "".equals(sessionToken)) {
            request.getSession().setAttribute("sessionToken", token);
        } else if (sessionToken.equals(token)) {
            flag = true;
        } else {
            request.getSession().setAttribute("sessionToken", token);
        }
        return flag;
    }


    /**
     * 返回提示信息
     *
     * @param msg
     */
    public void addMsg(String msg) {
        this.getRequest().setAttribute("msg", msg);
    }


    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public PageInfo getPageInfo() {
        return pageInfo;
    }

    public void setPageInfo(PageInfo pageInfo) {
        this.pageInfo = pageInfo;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}
